#include "../include/dir.h"

int make_id_path(session_t *session, MYSQL *mysql, const char *destDir, int *finalID){
    char destDirCopy[MAX_PATH_LEN] = {0};
    strncpy(destDirCopy, destDir, sizeof(destDirCopy)-1);//strtok_r需要传入传出参数
   
    int stack[50] = {0}; //栈
    int top = 0; //栈顶下一存放位置
    
    char query[MAX_QUERY_LEN] = {0}; //SQL语句
    
    int ret = -1;   //用作返回值检查
    MYSQL_RES *table = NULL; //用于mysql_store_result
    MYSQL_ROW row = NULL;  //用于mysql_fetch_row
    
    //判断绝对 相对路径
    if(destDir[0] == '/'){
        //绝对路径
        //查找用户根目录id压栈
        sprintf(query, 
                "select id from file where username  = '%s' and parent_id = -1 and tomb = 0", 
                session->username);
        ret = mysql_query(mysql, query);
        MYSQL_QUERY_CHECK(ret, mysql);
        
        table = mysql_store_result(mysql);
        MYSQL_STORE_RESULT_CHECK(table, mysql);
        
        row = mysql_fetch_row(table);
        //拿到根目录id->压栈
        stack[top] = atoi(row[0]);
        top++;
        mysql_free_result(table);
    }
    else{
        //相对路径
        //将当前目录id压栈->遇到 cd ../xx/xx ->弹栈->根据弹栈id找到parent_id压栈
        stack[top] = session->curDirID;
        top++;
    }

    //strtok_r切割->查id->压栈
    char *saveptr;//strtok_r所需的维护指针
    char *token = strtok_r(destDirCopy, "/", &saveptr);
    while(token){
        if(strcmp(token, ".") == 0){
            //无视
        }
        else if(strcmp(token, "..") == 0){
            //根据当前目录id查parent_id
            //不管是绝对 相对路径 开始时栈里都是只有一个
            if(top > 1){ 
                top--;
            }
            else{
                int currentID = stack[top-1];
                sprintf(query,
                        "select parent_id from file where id = %d and username = '%s' and tomb = 0",
                        currentID, session->username);
                ret = mysql_query(mysql, query);
                MYSQL_QUERY_CHECK(ret, mysql);

                table = mysql_store_result(mysql);
                MYSQL_STORE_RESULT_CHECK(table, mysql);

                row = mysql_fetch_row(table);
                int parent_id = atoi(row[0]);
                mysql_free_result(table);
                if(parent_id == -1){
                    //说明当前是 根目录/ 则什么都不做
                }
                else{
                    // 压入父目录ID
                    //top--;
                    stack[top-1] = parent_id;
                }
            }
        }
        else{
            //普通目录->以当前栈顶为parent_id->找到对应目录id
            int parent_id = stack[top-1];
            sprintf(query,
                    "select id from file where username = '%s'and parent_id = %d and filename = '%s' and filetype = 'd' and tomb = 0",
                    session->username, parent_id, token);
            ret = mysql_query(mysql, query);
            MYSQL_QUERY_CHECK(ret, mysql);
            table = mysql_store_result(mysql);
            MYSQL_STORE_RESULT_CHECK(table, mysql);
            if(mysql_num_rows(table) == 0){
                //select结果一行都没有 代表路径是错误的
                mysql_free_result(table);
                return -1;
            }
            //正常拿到->压栈
            row = mysql_fetch_row(table);
            int child_id = atoi(row[0]);
            stack[top] = child_id;
            top++;
        }
        token = strtok_r(NULL, "/", &saveptr);
    }
    *finalID = stack[top-1];
    return 0;
}
